<?php

class Utilitarios_RecursosController extends Zend_Rest_Controller {

	public function init() {
		/* Initialize action controller here */
		$ajaxContext = $this->_helper->contextSwitch();
		$ajaxContext->setAutoJsonSerialization(true);
		$ajaxContext
		->addActionContext('index', array('json'))
		->addActionContext('get', array('json'))
		->addActionContext('post', array('json'))
		->addActionContext('put', array('json'))
		->addActionContext('delete', array('json'))
		->initContext('json');
	}

	public function indexAction(){
		$model=new Application_Model_Utilitarios_Recurso();
		$result=$model->index();
		foreach($result['data'] as &$row){
			if(isset($row['chamadas']))
				foreach($row['chamadas'] as &$chamadas){
				$cresult=$model->get($chamadas['chamada_id']);
				if($cresult['success'])if($cresult['total']==1)$chamadas['chamada']=$cresult['data'];
			}
// 			if(isset($row['funcoes']))
// 				foreach($row['funcoes'] as &$funcoes){
// 				$cresult=$model->get($funcoes['funcao_id']);
// 				if($cresult['success'])if($cresult['total']==1)$funcoes['funcao']=$cresult['data'];
// 			}
		}
		print Zend_Json::encode($result);
		exit;
	}

	public function getAction(){
		$model=new Application_Model_Utilitarios_Recurso();
		$result=$model->get($this->getRequest()->getParam('id'));
		$row=&$result['data'];
		if(isset($row['chamadas']))
			foreach($row['chamadas'] as &$chamadas){
			$cresult=$model->get($chamadas['chamada_id']);
			if($cresult['success'])if($cresult['total']==1)$chamadas['chamada']=$cresult['data'];
		}
// 		if(isset($row['funcoes']))
// 			foreach($row['funcoes'] as &$funcoes){
// 			$cresult=$model->get($funcoes['funcao_id']);
// 			if($cresult['success'])if($cresult['total']==1)$funcoes['funcao']=$cresult['data'];
// 		}
		
		print Zend_Json::encode($result);
		exit;
	}

	public function postAction(){
		$post = Zend_Json::decode($this->getRequest()->getRawBody());
		if(!$post)
			$post = $this->getRequest()->getPost();
		if(count($_FILES))
			foreach($_FILES as $i=>$file)
			$post[$i] = $file;

		$post['chamadas']=Zend_Json::decode($this->getRequest()->getParam('chamadas'));
// 		$post['funcoes']=Zend_Json::decode($this->getRequest()->getParam('funcoes'));

		$model=new Application_Model_Utilitarios_Recurso();
		if(count($_FILES))
			foreach($_FILES as $i=>$file)
			$post[$i] = $file;
		$result=$model->post($post);
		print Zend_Json::encode($result);
		exit;
	}

	public function putAction(){
		$id = $this->getRequest()->getParam('id');
		$post = Zend_Json::decode($this->getRequest()->getRawBody());
		//$post['id'] = $id;
		if(!$post)
			$post = $this->getRequest()->getPost();
		if(count($_FILES))
			foreach($_FILES as $i=>$file)
			$post[$i] = $file;

		$post['chamadas']=Zend_Json::decode($this->getRequest()->getParam('chamadas'));
// 		$post['funcoes']=Zend_Json::decode($this->getRequest()->getParam('funcoes'));

		$model=new Application_Model_Utilitarios_Recurso();
		$result=$model->put($id,$post);
		print Zend_Json::encode($result);
		exit;
	}

	public function deleteAction(){
		$id = $this->getRequest()->getParam('id');

		$model=new Application_Model_Utilitarios_Recurso();
		$result=$model->delete($id);

		print Zend_Json::encode($result);
		exit;
	}

}